package com.qf.face.controller;

import com.qf.base.dto.face.FaceDTO;
import com.qf.base.result.R;
import com.qf.face.service.FaceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 30909
 */
@RestController
@RequestMapping("qf/face")
public class FaceController {

    @Resource
    private FaceService faceService;

    @PostMapping("user")
    public R addFace(@RequestBody FaceDTO faceDTO) {
        String face = faceDTO.getFace();
        Long userId = faceDTO.getUserId();
        Integer serviceId = faceDTO.getServiceId();
        if (serviceId == null) {
            //请求屎非法的
            return R.error(505, "请指定 serviceId");
        }
        boolean result = faceService.addFace(userId, face, serviceId);
        if (result) {
            return R.ok("人脸录入ok");
        }else {
            return R.error("人脸失败");
        }

    }


    @PutMapping("user")
    public R editFace(@RequestBody FaceDTO faceDTO) {
        String face = faceDTO.getFace();
        Long userId = faceDTO.getUserId();
        Integer serviceId = faceDTO.getServiceId();

        if (serviceId == null) {
            //请求是非法的
            return R.error(505, "请指定 serviceId");
        }
        //修改
        boolean result = faceService.editFace(userId, face, serviceId);
        if (result) {
            return R.ok("人脸修改ok");
        }else {
            return R.error("人脸修改失败");
        }

    }

    @DeleteMapping("user")
    public R delFace(@RequestBody FaceDTO faceDTO) {
        String face = faceDTO.getFace();
        Long userId = faceDTO.getUserId();
        Integer serviceId = faceDTO.getServiceId();
        if (serviceId == null) {
            //请求屎非法的
            return R.error(505, "请指定 serviceId");
        }
        faceService.removeFace(userId, serviceId);

        return R.ok("人脸删除ok");
    }

    @PostMapping("search")
    public R search(@RequestBody FaceDTO faceDTO) {
        String face = faceDTO.getFace();

        Integer serviceId = faceDTO.getServiceId();
        if (serviceId == null) {
            //请求屎非法的
            return R.error(505, "请指定 serviceId");
        }
        Long userId = faceService.search(face, serviceId);
        if (userId != null) {
            //查询成功
            return R.ok("打卡成功！")
                    .setResult("userid", userId);
        } else {
            return R.error("人脸不存在");
        }

    }


}
